-
Notifications
You must be signed in to change notification settings - Fork 352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: created new type for API errors, created and implemented type predicates #6877
Conversation
…predicates Created APIError interface in `command-helpers.ts`. Created two type predicates `isAPIError` and `errorHasStatus`. Implemented predicates across the code base. Co-authored-by: Ben Hancock <benhancock859@gmail.com>
Deleted remaining TSFIXME comments that were not needed anymore due to adding `unknown` type to custom `error` function. Co-authored-by: Ben Hancock <benhancock859@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a simpler version of adding the type and it's less for the users to remember to implement as they add more error catching.
src/utils/command-helpers.ts
Outdated
interface APIError extends Error { | ||
status: number | ||
message: string | ||
} | ||
|
||
export const isAPIError = (errorObject: unknown): errorObject is APIError => | ||
errorObject instanceof Error && 'status' in errorObject && 'message' in errorObject | ||
|
||
export const errorHasStatus = (errorObject: unknown, statusCode: number) => | ||
isAPIError(errorObject) && errorObject.status === statusCode |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
interface APIError extends Error { | |
status: number | |
message: string | |
} | |
export const isAPIError = (errorObject: unknown): errorObject is APIError => | |
errorObject instanceof Error && 'status' in errorObject && 'message' in errorObject | |
export const errorHasStatus = (errorObject: unknown, statusCode: number) => | |
isAPIError(errorObject) && errorObject.status === statusCode | |
export interface APIError extends Error { | |
status: number | |
message: string | |
} |
src/utils/dev.ts
Outdated
isAPIError(error_) | ||
? error(`Failed retrieving addons for site ${chalk.yellow(site.id)}: ${error_.message}. ${ERROR_CALL_TO_ACTION}`) | ||
: error(error_) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isAPIError(error_) | |
? error(`Failed retrieving addons for site ${chalk.yellow(site.id)}: ${error_.message}. ${ERROR_CALL_TO_ACTION}`) | |
: error(error_) | |
error(`Failed retrieving addons for site ${chalk.yellow(site.id)}: ${(error_ as APIError).message}. ${ERROR_CALL_TO_ACTION}`) | |
Co-authored-by: Daniel Lew <51924260+DanielSLew@users.noreply.github.com>
Co-authored-by: Daniel Lew <51924260+DanielSLew@users.noreply.github.com>
Co-authored-by: Ben Hancock <benhancock859@gmail.com>
Co-authored-by: Ben Hancock <benhancock859@gmail.com>
@DanielSLew Changed to type assertions for APIError |
🎉 Thanks for submitting a pull request! 🎉
Summary
Fixes conversion to TypeScript
Previously, most invocations of the custom
error
function needed to have a TypeScript ignore comment due to theerror_
object being passed to the function being of typeunknown
. Because theerror
function already takeserror_
arguments that are of various different shapes, addingunknown
to the parameter's type union allowed us to remove these TypeScript ignore comments without sacrificing type safety. Added a new typeAPIError
and type predicates for custom type guarding of errors that are a result of an internal API call.For us to review and ship your PR efficiently, please perform the following steps:
passes our tests.
A picture of a cute animal (not mandatory, but encouraged)